package com.handpay.zztong.hp.accountcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.merchantinfo.MerchantInfo;
import com.handpay.zztong.hp.main.model.merchantinfo.MerchantInfoModel;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.protocol.UCProtocolKey;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.handpay.zztong.hp.utils.BankUtils;

import java.util.Hashtable;

/**
 * 收款卡管理
 *
 * @author jjyang
 */
public class BankCardManagerActivity extends ZZTongActivity {

    private ImageView bank_type_logo;
    private Button modify_card;
    private TextView bank_name, cardholder_name, cardNotv, bank_address;
    private String mBankCode, mSubBankCode;
    private String mBankProvinceId, mBankAreaId;
    private String mBankProvinceName, mBankAreaName, mSubBankName;
    private String cardNum;

    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Receivables_management), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bank_card_manager);
        super.onCreate(savedInstanceState);

        bank_name = (TextView) findViewById(R.id.bank_name);
        cardholder_name = (TextView) findViewById(R.id.cardholder_name);
        cardNotv = (TextView) findViewById(R.id.cardNotv);
        bank_address = (TextView) findViewById(R.id.bank_address);
        bank_type_logo = (ImageView) findViewById(R.id.bank_type_logo);
        modify_card = (Button) findViewById(R.id.modify_cardbt);
        modify_card.setOnClickListener(this);
        if (AccountUtils.getAccountStatus(this) != AccountUtils.AccountStatus.SUCCESS) {
            modify_card.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    protected void onResume() {
        super.onResume();
        getMerchantInfo();
    }

    private void getMerchantInfo() {
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
        params.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);

        new MerchantInfoModel().doMerchantInfoPost(this, params, new BusinessEngineListener<MerchantInfo>() {
            @Override
            public void callbackBusinessResponse(MerchantInfo bean) {
                String bankName = bean.getBankName();
                mSubBankName = bean.getSubBranchName();
                cardNum = bean.getCardNum();
                String name = bean.getName();
                mBankProvinceId = bean.getProvinceId();
                mBankProvinceName = bean.getProvinceName();
                if (!TextUtils.isEmpty(mBankProvinceId)) {
                    mBankProvinceName = SecureManager.getInstance().des(mBankProvinceName, 0, null);
                }
                mBankAreaId = bean.getAreaId();
                mBankAreaName = bean.getAreaName();
                if (!TextUtils.isEmpty(mBankAreaId)) {
                    mBankAreaName = SecureManager.getInstance().des(mBankAreaName, 0, null);
                }

                mBankCode = bean.getBankCode();
                mSubBankCode = bean.getSubBankCode();
                name = SecureManager.getInstance().des(name, 0, null);
                bankName = SecureManager.getInstance().des(bankName, 0, null);
                HPLog.e("BankCardManagerActivity", "bankName:" + bankName);
                cardNum = SecureManager.getInstance().des(cardNum, 0, null);
                if (!TextUtils.isEmpty(mSubBankCode)) {
                    mSubBankName = SecureManager.getInstance().des(mSubBankName, 0, null);
                    bank_address.setText(mSubBankName);
                } else {
                    bank_address.setText("");
                }
                cardholder_name.setText(name);
                bank_name.setText(bankName);
                cardNotv.setText(CommonUtils.formattingCard(cardNum));
                bank_type_logo.setImageResource(BankUtils.getBankIcon(bankName));

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.modify_cardbt) {
            if (mBankCode == null || mBankCode.trim().length() == 0) {
                showAlertDialog(this, getString(R.string.tip), getString(R.string.update_Bank_message), true, null);
                return;
            }
            Intent intent = new Intent(this, BankCardModifyActivity.class);
            intent.putExtra(UCProtocolKey.KEY_NAME, cardholder_name.getText());
            intent.putExtra(UCProtocolKey.KEY_BANKCODE, mBankCode);
            intent.putExtra(UCProtocolKey.KEY_BANKNAME, bank_name.getText());
            intent.putExtra(UCProtocolKey.KEY_CARDNUM, CommonUtils.formattingCard(cardNum));
            if (!TextUtils.isEmpty(mBankProvinceId) && !TextUtils.isEmpty(mBankProvinceName)) {
                intent.putExtra(UCProtocolKey.KEY_PROVIVCEID, mBankProvinceId);
                intent.putExtra(UCProtocolKey.KEY_PROVINCENAME, mBankProvinceName);
            }
            if (!TextUtils.isEmpty(mBankAreaId) && !TextUtils.isEmpty(mBankAreaName)) {
                intent.putExtra(UCProtocolKey.KEY_AREAID, mBankAreaId);
                intent.putExtra(UCProtocolKey.KEY_AREANAME, mBankAreaName);
            }
            if (!TextUtils.isEmpty(mSubBankCode) && !TextUtils.isEmpty(mSubBankName)) {
                intent.putExtra(UCProtocolKey.KEY_SUBBANKNAME, mSubBankName);
                intent.putExtra(UCProtocolKey.KEY_SUBBANKCODE, mSubBankCode);
            }
            this.startActivity(intent);
        }
    }

}
